<?php
/**
 * User: krisbuist
 * Date: 2/1/12
 * Time: 12:51 PM
 */
class LikeGenerator extends Controller
{
    protected $step = 50;

    protected $numberOfUsers;
    protected $numberOfComments;
    protected $numberOfPosts;

    protected $avgRatingsPerComment = 2;
    protected $avgRatingsPerPost = 5;

    protected $totalNumberOfCommentRatings;
    protected $totalNumberOfPostRatings;

    public function __construct()
    {
        parent::__construct();
        $this->numberOfComments = $this->em->createQuery("SELECT COUNT(c) FROM Comment c")->getSingleScalarResult();
        $this->numberOfPosts = $this->em->createQuery("SELECT COUNT(p) FROM Post p")->getSingleScalarResult();
        $this->numberOfUsers = $this->em->createQuery("SELECT COUNT(u) FROM User u")->getSingleScalarResult();

        $this->totalNumberOfCommentRatings = $this->avgRatingsPerComment * $this->numberOfComments;
        $this->totalNumberOfPostRatings = $this->avgRatingsPerPost * $this->numberOfPosts;
    }

    public function index()
    {
        $this->generatePostRatings();
    }


    public function generatePostRatings()
    {
        try
        {
            for ($i = 0; $i < $this->step; $i++)
            {
                $post = $this->em->find('Post', rand(1, $this->numberOfPosts));
                $postRating = $this->generateRandomPostRating($post);
                $this->em->persist($postRating);
            }

            $this->em->flush();
        }
        catch (PDOException $e)
        {
            echo $e;
        }
        $this->forwardBrowserIfNotFinishedPostRating();
    }

    public function forwardBrowserIfNotFinishedPostRating()
    {
        $numberOfPostRatings = $this->em->createQuery("SELECT COUNT(pr) FROM Post_Rating pr")->getSingleScalarResult();
        if ($numberOfPostRatings >= $this->totalNumberOfPostRatings)
        {
            echo '<meta http-equiv="refresh" content="0;url=/generators/likeGenerator/generateCommentRatings">';
            echo 'Finished inserting postRatings! Next up: CommentRatings';
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=/generators/likeGenerator/generatePostRatings/">';
            echo '<title>' . $numberOfPostRatings . '</title>';
        }
    }

    /**
     * @return User
     */
    private function getRandomUser()
    {
        $author = null;
        while ($author == null)
        {
            $author = $this->em->find('User', rand(1, $this->numberOfUsers));
        }
        return $author;
    }

    /**
     * @param $post Post
     * @return Post_Rating
     */
    private function generateRandomPostRating($post)
    {
        $postRating = new Post_Rating();
        $postRating->setPost($post);
        $postRating->setUser($this->getRandomUser());
        $rating = rand(0, 1) == 1 ? -1 : 1;
        $postRating->setRating($rating);
        return $postRating;
    }

    public function generateCommentRatings()
    {
        try
        {
            for ($i = 0; $i < $this->step; $i++)
            {
                $comment = $this->em->find('Comment', rand(1, $this->numberOfComments));
                $commentRating = $this->generateRandomCommentRating($comment);
                $this->em->persist($commentRating);
            }

            $this->em->flush();
        }
        catch (PDOException $e)
        {
            echo $e;
        }


        $this->forwardBrowserIfNotFinishedCommentRating();
    }

    public function forwardBrowserIfNotFinishedCommentRating()
    {
        $numberOfCommentRatings = $this->em->createQuery("SELECT COUNT(cr) FROM Comment_Rating cr")->getSingleScalarResult();
        if ($numberOfCommentRatings >= $this->totalNumberOfCommentRatings)
        {
            echo 'Finished inserting commentRatings!';
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=/generators/likeGenerator/generateCommentRatings/">';
            echo '<title>' . $numberOfCommentRatings . '</title>';
        }
    }

    /**
     * @param $comment Comment
     * @return Comment_Rating
     */
    private function generateRandomCommentRating($comment)
    {
        $commentRating = new Comment_Rating();
        $commentRating->setComment($comment);
        $commentRating->setUser($this->getRandomUser());
        $rating = rand(0, 1) == 1 ? -1 : 1;
        $commentRating->setRating($rating);
        return $commentRating;
    }


}
